<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $userid
 * @property string $username
 * @property string $password
 */
class User extends UserBase
{
    public $oldpass = '';
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('userid, username', 'required'),
			array('userid', 'numerical', 'integerOnly'=>true),
			array('username, password', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('userid, username, password', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'userid' => 'Userid',
			'username' => 'Username',
			'password' => 'Password',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('userid',$this->userid);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
        
        public function beforeSave() {
            if ($this->password != 'NULL'){
                if (strlen($this->password) < 6){
                    $this->addError('password', 'Password Can Not Less Than 6 Letters');
                    return false;
                } 

                if ($this->isNewRecord){
                    if (md5(md5($_POST['User']['repassword'], $_POST['User']['username'])) != $this->password){
                        $this->addError('password', 'Password And Repassword Does  Not Match');
                        return false;
                    }
                }else{  //update pass
                    //check old pass is correct?
                    if (md5(md5($_POST['User']['password'], $_POST['User']['username'])) != $this->oldpass){
                        $this->addError('password', 'Current Password Is Incorrect');
                        return false;
                    }
                    //check new pass valid
                    if (strlen($_POST['User']['newpassword']) < 6){
                        $this->addError('password', 'New Password Can Not Less Than 6 Letters');
                        return false;
                    }

                    if (md5(md5($_POST['User']['renewpassword'], $_POST['User']['username'])) != $this->password){
                        $this->addError('password', 'Retype New Password Is Incorrect');
                        return false;
                    }
                }
            }
                        
            return true;                    
        }
}